﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transcend.DP3.Helper
{
	public class SystemHelper
	{
		/// <summary>
		/// 取ip地址
		/// </summary>
		/// <returns></returns>
		public static string GetInterNetworkAddress()
		{
			string host = System.Net.Dns.GetHostName();
			string localAddr = "?";
			var ip = System.Net.Dns.GetHostEntry(host);
			foreach (var _IPAddress in ip.AddressList)
			{
				if (_IPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
				{
					localAddr = _IPAddress.ToString();
				}
			}
			return localAddr;
		}

		/// <summary>
		/// 取mac地址
		/// </summary>
		/// <param name="ip"></param>
		/// <returns></returns>
		public static string GetMAC(string ip)
		{

			var query = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
			var queryCollection = query.Get();
			bool Found = false;
			foreach (var _ManagementObject in queryCollection)
			{
				if (_ManagementObject["IPAddress"] != null)
				{
					string _IPAddress;
					_IPAddress = string.Join(".", (string[])_ManagementObject["IPAddress"]);

					if (!_IPAddress.Equals(""))
					{
						if (_IPAddress.Equals(ip.Trim()))
						{
							Found = true;
						}
					}

					if (Found == true)
					{
						if (_ManagementObject["macaddress"] != null)
						{
							if (!_ManagementObject["macaddress"].Equals(""))
							{
								return (string)_ManagementObject["macaddress"];
							}
						}
					}
					else
					{
						Found = false;
					}
				}
			}

			return "";
		}
	}
}
